<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link rel="shortcut icon" href="favicon.ico">
    <link href="__PUBLIC__/admin/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="__PUBLIC__/admin/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="__PUBLIC__/admin/css/style.css?v=4.1.0" rel="stylesheet">
</head>

<body class="gray-bg">
<style>
    input.error{
        border:1px solid red;
    }
    span.error{
        padding-top:10px;
        color: #f00;
        font-size:12px;
    }
</style>
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>更换登录手机号</h5>
                </div>
                <div class="ibox-content">
                    <form method="post" class="form-horizontal" id="form">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">新手机号：</label>
                            <div class="col-sm-6">
                                <input type="text" style="width:61%;" class="form-control" name="phone" placeholder="">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">短信验证码：</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control" name="phonecode" style="width:61%; float:left; margin-bottom:15px;">
                                <button type="button" class="btn btn-primary btn-primary" id="btnSendCode" style="float:left;margin-left:10px;">发送验证码</button>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button class="btn btn-primary" type="submit" id="doSubmit" style="margin-right:100px;">保存</button>
                                <button type="reset" class="btn btn-default">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 全局js -->
<script src="__PUBLIC__/admin/js/jquery.min.js?v=2.1.4"></script>
<script src="__PUBLIC__/admin/js/bootstrap.min.js?v=3.3.6"></script>
<!-- 自定义js -->
<script src="__PUBLIC__/admin/js/plugins/layer/layer.min.js"></script>
<script src="__PUBLIC__/admin/js/plugins/validate/jquery.validate.min.js"></script>
<!-- iCheck -->
<script src="__PUBLIC__/admin/js/plugins/iCheck/icheck.min.js"></script>
<script src="__PUBLIC__/admin/js/common/ajax.js"></script>

<script>
    var editphoneUrl = "{:url('shop_admin/editphone')}";
    var editphonecode = "{:url('SmsCode/send')}";

    var InterValObj; //timer变量，控制时间
    var count = {$messtime}; //间隔函数，1秒执行
    var curCount;//当前剩余秒数



    $(function(){
        $('#btnSendCode').click(function(){
            $(this).attr("disabled", "true");

            var phone = $('input[name=phone]').val();
            if(phone == ''){
                layer.msg('请输入手机号码', {icon: 2,time: 2000});
                $(this).removeAttr("disabled");//启用按钮
                return false;
            }

            if(!(/^1[3456789]\d{9}$/.test(phone))){
                layer.msg('请输入正确的手机号码', {icon: 2,time: 2000});
                $(this).removeAttr("disabled");//启用按钮
                return false;
            }

            layer.load(2);

            //向后台发送处理数据
            $.ajax({
                url:editphonecode,
                type:'POST',
                data:{'type':6,'phone': phone},
                dataType:'json',
                success:function(data){
                    if(data.status == 1){
                        layer.closeAll('loading');
                        layer.msg(data.mess, {icon: 1,time: 1000});
                        curCount = count;
                        $("#btnSendCode").css({backgroundColor:'#f5f5f5',color:'#333'});
                        //设置button效果，开始计时
                        $("#btnSendCode").text("重发("+curCount+")s");
                        InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
                    }else{
                        layer.closeAll('loading');
                        $("#btnSendCode").removeAttr("disabled");//启用按钮
                        layer.msg(data.mess, {icon: 2,time: 2000});
                    }
                },
                error:function(){
                    layer.closeAll('loading');
                    $("#btnSendCode").removeAttr("disabled");//启用按钮
                    layer.msg('操作失败，请重试', {icon: 2,time: 2000});
                }
            });
        });

        //手机号验证
        jQuery.validator.addMethod("phone", function(value, element){
            var tel = /^1[3456789]\d{9}$/;
            return this.optional(element) || (tel.test(value));
        }, "手机格式不正确");

        var icon = "<i class='fa fa-times-circle'></i>&nbsp;&nbsp;";
        $('#form').validate({
            errorElement : 'span',
            debug: true,//只验证不提交表单
            //layer ajax提交表单
            submitHandler:function(){
                // 序列化 表单数据 后提交 ，太简洁了
                post(editphoneUrl,'POST',$('#form').serialize(),1);
                return false;
            },//这是关键的语句，配置这个参数后表单不会自动提交，验证通过之后会去调用的方法

            rules:{
                phone:{
                    required:true,
                    phone:true
                },
                phonecode:{required:true},
            },

            messages:{
                phone:{
                    required:icon+'必填',
                    phone:icon+'手机号格式不正确'
                },
                phonecode:{required:icon+'必填'},
            }
        });
    });

    function cl(){
        location.href="{:url('login/loginout')}";
    }

    //timer处理函数
    function SetRemainTime() {
        if (curCount == 0) {
            window.clearInterval(InterValObj);//停止计时器
            $("#btnSendCode").removeAttr("disabled");//启用按钮
            $("#btnSendCode").css({backgroundColor:'#1992FC',color:'#FFF'});
            $("#btnSendCode").text("重发");
        }
        else {
            curCount--;
            $("#btnSendCode").text("重发("+curCount+")s");
        }
    }

</script>

</body>
</html>
